<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class My extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * 
     */
    public function index()
    {
        $people = model('People')->people($this->request->post('openid'));
        return json_encode($people);
    }
    //收货地址
    public function address(){
        $openid=$this->request->post("openid");
        $findid=Db::name("people")->where(array("openid"=>$openid))->find();
        $res = Db::name('address')->where('people_id',$findid['id'])->order("isdefault desc")->select();
        return json_encode(['res'=>$res]);
    }
    //添加收货地址
    public function add_address(){
        $openid=$this->request->post("openid");
        $findid=Db::name("people")->where(array("openid"=>$openid))->find();
        if (!$findid){
            return json_encode(array('status'=>0,'err'=>'网络异常.'.__LINE__));
            exit();
        }
        $data = array();
        $data['name'] = trim($this->request->post('name'));
        $data['phone'] = trim($this->request->post('phone'));
        $data['sheng'] =$this->request->post('sheng');
        $data['city'] = $this->request->post('city');
        $data['quyu'] = $this->request->post('quyu');
        $data['address'] = $this->request->post('adds');
        $data['people_id'] = $findid['id'];
        if($this->request->post('isdefault')=='true' || $this->request->post('isdefault') == '1') $data['isdefault']=1;
        else $data['isdefault']=0;
        if (!$data['name'] || !$data['phone'] || !$data['address']) {
            return json_encode(array('status'=>0,'err'=>'请先完善信息后再提交.'));
            exit();
        }
        if (!$data['sheng'] || !$data['city'] || !$data['quyu']) {
            return json_encode(array('status'=>0,'err'=>'请选择省市区.'));
            exit();
        }
        $check_id = Db::name('address')->field("id")->where($data)->find();
        if ($check_id) {
            return json_encode(array('status'=>0,'err'=>'该地址已经添加了.'));
            exit();
        }
        $data['address_xq'] = "[".$this->request->post('xq')."]";
        if($this->request->post('aid')){
            $res = Db::name('address')->where(array("id"=>$this->request->post('aid')))->update($data);
        }else{
            $res = Db::name('address')->insert($data);
        }
        if ($res) {
            $arr = array();
            $arr['id'] = $res;
            $arr['name'] = $data['name'];
            $arr['phone'] = $data['phone'];
            $arr['address'] = $data['address'];
            $find= Db::name('address')->field("id")->where($data)->find();
            Db::name('address')->where("id != {$find['id']}")->update(array("isdefault"=>0));
            return json_encode(array('status'=>1,'add_arr'=>$arr));
            exit();
        }else{
            return json_encode(array('status'=>0,'err'=>'操作失败.'));
            exit();
        }
    }
    // 我的档案
    public function myArchives(){
        $people = model('People')->people($this->request->post('openid'));
        $res = Db::name('archives')->where('people_id',$people['id'])->find();
        if($res){
            if($res['zysrc_images']){
                $zysrc = str_replace('/uploads','http://zudian.cn/uploads',$res['zysrc_images']);
                $res['zysrcs'] = explode(',',$res['zysrc_images']);
                $res['zysrc'] = explode(',',$zysrc);
            }else{
                $res['zysrcs'] = [];
                $res['zysrc'] = [];
            }

            if($res['zxsrc_images']){
                $zxsrc = str_replace('/uploads','http://zudian.cn/uploads',$res['zxsrc_images']);
                $res['zxsrcs'] = explode(',',$res['zxsrc_images']);
                $res['zxsrc'] = explode(',',$zxsrc);
            }else{
                $res['zxsrcs'] = [];
                $res['zxsrc'] = [];
            }

            if($res['xdsrc_images']){
                $xdsrc = str_replace('/uploads','http://zudian.cn/uploads',$res['xdsrc_images']);
                $res['xdsrcs'] = explode(',',$res['xdsrc_images']);
                $res['xdsrc'] = explode(',',$xdsrc);
            }else{
                $res['xdsrcs'] = [];
                $res['xdsrc'] = [];
            }

            if($res['zhsrc_images']){
                $zhsrc = str_replace('/uploads','http://zudian.cn/uploads',$res['zhsrc_images']);
                $res['zhsrcs'] = explode(',',$res['zhsrc_images']);
                $res['zhsrc'] = explode(',',$zhsrc);
            }else{
                $res['zhsrcs'] = [];
                $res['zhsrc'] = [];
            }

            if($res['xdisrc_images']){
                $xdisrc = str_replace('/uploads','http://zudian.cn/uploads',$res['xdisrc_images']);
                $res['xdisrcs'] = explode(',',$res['xdisrc_images']);
                $res['xdisrc'] = explode(',',$xdisrc);
            }else{
                $res['xdisrcs'] = [];
                $res['xdisrc'] = [];
            }

            if($res['tdsrc_images']){
                $tdsrc = str_replace('/uploads','http://zudian.cn/uploads',$res['tdsrc_images']);
                $res['tdsrcs'] = explode(',',$res['tdsrc_images']);
                $res['tdsrc'] = explode(',',$tdsrc);
            }else{
                $res['tdsrcs'] = [];
                $res['tdsrc'] = [];
            }

            if($res['btsrc_images']){
                $btsrc = str_replace('/uploads','http://zudian.cn/uploads',$res['btsrc_images']);
                $res['btsrcs'] = explode(',',$res['btsrc_images']);
                $res['btsrc'] = explode(',',$btsrc);
            }else{
                $res['btsrcs'] = [];
                $res['btsrc'] = [];
            }

        }
        return json_encode($res);
    }
    //微信端上传图片
    public function uploadimg()
    {
        $file = request()->file('file');
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                $file = $info->getSaveName();
                $res = "/uploads/".$file;
                return $res;
            }
        }
    }
    public function baoc(){
        $params = $this->request->post();
        $people = model('People')->people($params['openid']);
        $count = Db::name('archives')->where('people_id',$people['id'])->count();
        if($count > 0){
            array_shift($params);
            $res = Db::name('archives')->where('people_id',$people['id'])->update($params);
        }else{
            array_shift($params);
            $params['people_id'] = $people['id'];
            $params['createtime'] = time();
            $res = Db::name('archives')->insert($params);
        }

        return $res?200:400;
    }
}
